import subprocess
import os


def run(cmd, stdout_file=None, stderr_file=None):
    try:
        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()

        if stdout_file:
            with open(stdout_file, 'wb') as f:
                f.write(stdout)

        if stderr_file:
            with open(stderr_file, 'wb') as f:
                f.write(stderr)

        return process.returncode

    except Exception as e:
        print(f"Error executing command: {e}")
        return 1
